import rsa
import time

class RSATools:
    def __init__(self):
        (self.pubkey, self.privkey) = rsa.newkeys(512)
        # print("pub: ", pubkey)
        # print("priv: ", privkey)
    
    def getPubkey(self):
        return self.pubkey
        
    # rsa加密
    def rsaEncrypt(self,str):
        # 明文编码格式
        content = str.encode('utf-8')
        # 公钥加密
        crypto = rsa.encrypt(content, self.pubkey)
        return (crypto, self.privkey)

    # rsa解密
    def rsaDecrypt(self,str, pk):
        # 私钥解密
        content = rsa.decrypt(str, pk)
        con = content.decode('utf-8')
        return con


if __name__ == '__main__':
    rsatool = RSATools()
    (crypto, privkey) = rsatool.rsaEncrypt("hello")
    print('加密后密文：')
    print(crypto)
    content = rsatool.rsaDecrypt(crypto, privkey)
    print('解密后明文：')
    print(content)